<?php
class Model_MapperActualite implements Model_IMapper
{
	private $table = null;
	
	public function __construct($str_table, $pdo)
	{
		$this->table = new Model_TableActualite();
		$this->table->setConnexion($pdo);
		$this->table->init();
	}
	
	public function getAll($begin = null, $nb = null)
	{
		if(is_null($begin) && is_null($nb))
		{
			$lignes = $this->table->fetchAll();
		}
		else
		{
			$lignes = $this->table->fetchAllLimit($begin, $nb);
		}
		
		$results = array();
		
		foreach($lignes as $ligne)
		{
			$entity = new Model_Actualite();
			$entity->setId_actualite($ligne['id_actualite']);
			$entity->setTitre($ligne['titre']);
			$entity->setImage($ligne['image']);
			$entity->setContenu($ligne['contenu']);
			$entity->setTags($ligne['tags']);
			$entity->setPublie($ligne['publie']);
			$entity->setDate_creation($ligne['date_creation']);
			$entity->setDate_modification($ligne['date_modification']);
			
			//recupere l'association
			$associationmap = new Model_MapperAssociation("association", $this->table->getConnexion());
			$association = $associationmap->getOne($ligne['id_association']);
			
			if(!is_null($association))
				$entity->setAssociation($association);
				
			//recupere l'evenement
			$evenementmap = new Model_MapperEvenement("evenement", $this->table->getConnexion());
			$evenement = $evenementmap->getOne($ligne['id_evenement']);
			
			if(!is_null($evenement))
				$entity->setEvenement($evenement);
				
			//recupere la commune
			$communemap = new Model_MapperCommune("commune", $this->table->getConnexion());
			$commune = $communemap->getOne($ligne['id_commune']);
			
			if(!is_null($commune))
				$entity->setCommune($commune);
					
			$results[] = $entity;
		}
		return $results;
	}
	
	public function getOne($id)
	{
		$ligne = $this->table->fetchOne($id);
		
		if(!empty($ligne['id_actualite']) )
		{
			$entity = new Model_Actualite();
			$entity->setId_actualite($ligne['id_actualite']);
			$entity->setTitre($ligne['titre']);
			$entity->setImage($ligne['image']);
			$entity->setContenu($ligne['contenu']);
			$entity->setTags($ligne['tags']);
			$entity->setPublie($ligne['publie']);
			$entity->setDate_creation($ligne['date_creation']);
			$entity->setDate_modification($ligne['date_modification']);
			
			//recupere l'association
			$associationmap = new Model_MapperAssociation("association", $this->table->getConnexion());
			$association = $associationmap->getOne($ligne['id_association']);
			
			if(!is_null($association))
				$entity->setAssociation($association);
				
			//recupere l'evenement
			$evenementmap = new Model_MapperEvenement("evenement", $this->table->getConnexion());
			$evenement = $evenementmap->getOne($ligne['id_evenement']);
			
			if(!is_null($evenement))
				$entity->setEvenement($evenement);
				
			//recupere la commune
			$communemap = new Model_MapperCommune("commune", $this->table->getConnexion());
			$commune = $communemap->getOne($ligne['id_commune']);
			
			if(!is_null($commune))
				$entity->setCommune($commune);
		}
		else 
		{
			$entity = null;
		}
		return $entity;
	}
	
	public function save(Model_IEntity $object)
	{
		$evenement = $object->getEvenement();
		
		if($object->getId_actualite() == -1)
		{
			$id = $this->table->insert(
				$object->getTitre(),
				$object->getImage(),
				$object->getContenu(),
				$object->getTags(),
				$object->getDate_creation()->format('Y-m-j H:i:s'),
				$object->getDate_modification()->format('Y-m-j H:i:s'),
				$object->getPublie(),
				$object->getAssociation()->getId_association(),
				$evenement?$evenement->getId_evenement():null,
				$object->getCommune()->getId_commune()
			);
			$object->setId_actualite($id);
		}
		else
		{
			$this->table->update(
				$object->getId_actualite(),
				$object->getTitre(),
				$object->getImage(),
				$object->getContenu(),
				$object->getTags(),
				$object->getDate_creation()->format('Y-m-j H:i:s'),
				$object->getDate_modification()->format('Y-m-j H:i:s'),
				$object->getPublie(),
				$object->getAssociation()->getId_association(),
				$evenement?$evenement->getId_evenement():null,
				$object->getCommune()->getId_commune()
			);
		}
	}
	
	public function delete(Model_IEntity $object)
	{
		$this->table->delete($object->getId_actualite());
	}
}